package com.ptszyxx.popose.module.main.user;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;
import com.ptszyxx.popose.BR;
import com.ptszyxx.popose.R;
import com.ptszyxx.popose.databinding.FragmentUserEditLabelBinding;
import com.ptszyxx.popose.module.main.user.adapter.UserEditLabelAdapter;
import com.ptszyxx.popose.module.main.user.vm.UserEditLabelVM;
import com.ysg.base.BaseFragment;
import com.ysg.http.data.AppViewModelFactory;
import com.ysg.http.data.entity.user.LabelItemEntity;
import com.ysg.http.data.entity.user.UserLabelResult;
import com.ysg.utils.YCollectionUtil;
import com.ysg.utils.YRecyclerViewUtil;
import com.ysg.utils.YStringUtil;
import com.ysg.utils.YToastUtil;

public class UserEditLabelFragment extends BaseFragment<FragmentUserEditLabelBinding, UserEditLabelVM> {
    private UserEditLabelAdapter adapterGX;
    private UserEditLabelAdapter adapterXQ;

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_user_edit_label;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public UserEditLabelVM initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(UserEditLabelVM.class);
    }

    @Override
    public void initData() {
        super.initData();
        viewModel.requestLabelList();
    }

    @Override
    public void initAdapter() {
        super.initAdapter();
        adapterGX = new UserEditLabelAdapter(viewModel);
        adapterXQ = new UserEditLabelAdapter(viewModel);

        YRecyclerViewUtil.initGrid(getContext(), binding.recyclerGx, adapterGX, 5);
        YRecyclerViewUtil.initGrid(getContext(), binding.recyclerXq, adapterXQ, 5);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.uc.onRefreshEvent.observe(this, o -> setAdapterUI((UserLabelResult)o));
        viewModel.uc.onSelectNumEvent.observe(this, o -> setSelectNumUI());
        viewModel.uc.onSaveEvent.observe(this, o -> onSave());
    }

    private void onSave() {
        String ids = "";
        String names = "";
        if (YCollectionUtil.isNotEmpty(adapterGX.getData())){
            for (LabelItemEntity item: adapterGX.getData()){
                if(YStringUtil.eq(1, item.getIsselect())){
                    ids +=  item.getId()+",";
                    names +=item.getName()+",";
                }
            }
        }
        if (YCollectionUtil.isNotEmpty(adapterXQ.getData())){
            for (LabelItemEntity item: adapterXQ.getData()){
                if(YStringUtil.eq(1, item.getIsselect())){
                    ids +=  item.getId()+",";
                    names +=item.getName()+",";
                }
            }
        }

        if (ids.length() == 0) {
            YToastUtil.showLong(R.string.user_edit_label_tip);
            return;
        }
        viewModel.requestLabel(YStringUtil.clearEndComma(ids), YStringUtil.clearEndComma(names));
    }

    /**
     * adapter
     */
    private void setAdapterUI(UserLabelResult result) {
        if (result != null && YCollectionUtil.isNotEmpty(result.getGexing())){
            for (LabelItemEntity item: result.getGexing()){
                if(YStringUtil.eq(1, item.getIsselect())){
                    viewModel.selectNum++;
                }
            }
            adapterGX.setList(result.getGexing());
        }
        if (result != null && YCollectionUtil.isNotEmpty(result.getXingqu())){
            for (LabelItemEntity item: result.getXingqu()){
                if(YStringUtil.eq(1, item.getIsselect())){
                    viewModel.selectNum++;
                }
            }
            adapterXQ.setList(result.getXingqu());
        }
        setSelectNumUI();
    }

    /**
     * 选择UI
     */
    private void setSelectNumUI() {
        binding.tvXuanzhong.setText(""+viewModel.selectNum);
    }
}
